{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " ## Tensor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import torch\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 指定Tensor大小，构建未初始化的Tensor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      " 0.0000e+00  0.0000e+00  1.2107e-42\n",
      " 0.0000e+00  7.0065e-45  1.8617e+25\n",
      " 5.0710e+37  6.0116e-43  0.0000e+00\n",
      " 0.0000e+00  7.0065e-45  1.8471e+25\n",
      " 5.0738e+37  6.0116e-43  0.0000e+00\n",
      "[torch.FloatTensor of size 5x3]\n",
      "\n"
     ]
    }
   ],
   "source": [
    "x = torch.Tensor(5, 3)\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 构建随机初始化的Tensor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      " 0.7139  0.4594  0.9405\n",
      " 0.7795  0.0622  0.6561\n",
      " 0.6556  0.2376  0.4662\n",
      " 0.1250  0.9931  0.7257\n",
      " 0.4262  0.9070  0.9179\n",
      "[torch.FloatTensor of size 5x3]\n",
      "\n",
      "<built-in method size of torch.FloatTensor object at 0x000001AD014A6348>\n"
     ]
    }
   ],
   "source": [
    "y = torch.rand(5, 3)\n",
    "print(y)\n",
    "print(y.size)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## torch.ones(size)构建指定size的全1矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\n",
       " 1  1  1\n",
       " 1  1  1\n",
       " 1  1  1\n",
       " 1  1  1\n",
       " 1  1  1\n",
       "[torch.FloatTensor of size 5x3]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "one = torch.ones(5, 3)\n",
    "one"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 下面表示的全是矩阵相加运算\n",
    "\n",
    "- `y + one` # 运算符相加\n",
    "- `torch.add(y,one)` # 相加\n",
    "- `torch.add(y,one,result)` # 输出结果到result\n",
    "- `y.add_(one)`  # 有`_`的操作表示替换当前值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\n",
       " 1.7139  1.4594  1.9405\n",
       " 1.7795  1.0622  1.6561\n",
       " 1.6556  1.2376  1.4662\n",
       " 1.1250  1.9931  1.7257\n",
       " 1.4262  1.9070  1.9179\n",
       "[torch.FloatTensor of size 5x3]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y + one"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\n",
       " 1.7139  1.4594  1.9405\n",
       " 1.7795  1.0622  1.6561\n",
       " 1.6556  1.2376  1.4662\n",
       " 1.1250  1.9931  1.7257\n",
       " 1.4262  1.9070  1.9179\n",
       "[torch.FloatTensor of size 5x3]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.add(y,one)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\n",
       " 1.7139  1.4594  1.9405\n",
       " 1.7795  1.0622  1.6561\n",
       " 1.6556  1.2376  1.4662\n",
       " 1.1250  1.9931  1.7257\n",
       " 1.4262  1.9070  1.9179\n",
       "[torch.FloatTensor of size 5x3]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result = torch.Tensor(5,3)\n",
    "torch.add(y,one,out=result)\n",
    "result"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 带有`_`的操作表示替换当前值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\n",
       " 1.7139  1.4594  1.9405\n",
       " 1.7795  1.0622  1.6561\n",
       " 1.6556  1.2376  1.4662\n",
       " 1.1250  1.9931  1.7257\n",
       " 1.4262  1.9070  1.9179\n",
       "[torch.FloatTensor of size 5x3]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y.add_(one)  \n",
    "y"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 张量也可以使用索引，切片等操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\n",
       " 1.7139  1.4594  1.9405\n",
       " 1.7795  1.0622  1.6561\n",
       "[torch.FloatTensor of size 2x3]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result[0: 2, :]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 如果要对Tensor做reshape/resize操作，使用`torch.view`\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "torch.Size([5, 3])\n",
      "torch.Size([15])\n",
      "torch.Size([3, 5])\n"
     ]
    }
   ],
   "source": [
    "print(result.size())\n",
    "\n",
    "view1 = result.view(15)\n",
    "view2 = result.view(-1,5) # 传入参数`-1`表推断，同时只能有一个-1\n",
    "\n",
    "print(view1.size())\n",
    "print(view2.size())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## **numpy和torch.tensor之间互转**\n",
    "\n",
    "- **ndarray--> tensor**: ndarry变量`x`，使用`torch.from_numpy(x)`\n",
    "- **tensor--> ndarray**: tensor变量`y`，使用`y.numpy()`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "np:[[0 1 2]\n",
      " [3 4 5]].format:<class 'numpy.ndarray'>\n",
      "np-->torch:\n",
      " 0  1  2\n",
      " 3  4  5\n",
      "[torch.IntTensor of size 2x3]\n",
      ".format:<class 'torch.IntTensor'>\n",
      "torch-->np:[[0 1 2]\n",
      " [3 4 5]].format:<class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "source": [
    "np1 = np.arange(6).reshape((2, 3)) # 创建一个ndarray\n",
    "ten1 = torch.from_numpy(np1)  # ndarray->torch\n",
    "np2 = ten1.numpy() # torch->ndarray\n",
    "\n",
    "print(\"np:{}.format:{}\".format(np1, type(np1)))\n",
    "print(\"np-->torch:{}.format:{}\".format(ten1, type(ten1)))\n",
    "print(\"torch-->np:{}.format:{}\".format(np2, type(np2)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## ndarray和tensor共享存储空间"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      " 1  2  3\n",
      " 4  5  6\n",
      "[torch.IntTensor of size 2x3]\n",
      "\n",
      "[[1 2 3]\n",
      " [4 5 6]]\n"
     ]
    }
   ],
   "source": [
    "ten1.add_(1)\n",
    "print(ten1)\n",
    "print(np2)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
